function OSSVideoController() {
  var that = this
  that.fileList = []
  that.bootstrap = function() {
    that.bindEvents()
  }
  that.bindEvents = function() {
    $("#fileInput").change(function(e) {
      console.error('OSSVideoController.bindEvents #fileInput', )
      // that.onSelectVideoSuccess(e.target.files)
      // e.target.files.forEach(console.log)
      for (var file of e.target.files) {
        that.onSelectVideo(file)
      }
    })
    $(".select-file-btn").on("click", function() {
      $("#fileInput").trigger("click")
    })
    $("body").on("click", ".delete-icon", function() {
      var id = $(this).attr("data-id")
      that.fileList = that.fileList.filter(e => e.id !== id)
      that.updateState()
    })
  }
  that.onSelectVideo = function(file) {
    that.fileList.push({
      file: file,
      status: "ready",
      id: "id_" + Math.random().toString().substring(2),
      url: that.getObjectURL(file)
    })
    that.updateState()
  }
  that.updateState = function() {
    that.toggleResultPanel()
    // 判断是否删除
    $(".video-list .item").each(function(idx, item) {
      var id = $(item).attr("id")
      if (!that.fileList.find(e => e.id === id)) {
        $(item).remove()
      }
    })
    that.fileList.forEach(function(item, idx) {
      var $item = $("#" + item.id)
      if ($item.length <= 0) {
        $item = $('' + 
            '<div id='+ item.id +' class="item '+ item.status +'">' +
            '  <div class="status">' +
            '    <a class="delete-icon" data-id="'+ item.id +'"></a>' +
            '  </div>' +
            '  <div class="img"><video src="'+ item.url +'"></video></div>' +
            '  <div class="success-icon"></div>' +
            '  <div class="progress-wrap">' +
            '    <img src="./images/uploading.png" alt="">' +
            '    <p>上传中 <span>10%</span></p>' +
            '  </div>' +
            '</div>' +
          '').insertBefore(".video-list .select-file-btn")
      }
    })
  }
  that.toggleResultPanel = function() {
    if (that.fileList.length <= 0) {
      $(".placeholder").removeClass("hidden")
      $(".result-wrapper").addClass("hidden")
    } else {
      $(".placeholder").addClass("hidden")
      $(".result-wrapper").removeClass("hidden")
    }
  }
  that.getObjectURL = function (file) {
    var url = null;
    if (window.createObjectURL != undefined) { // basic
        url = window.createObjectURL(file);
    } else if (window.URL != undefined) { // mozilla(firefox)
        url = window.URL.createObjectURL(file);
    } else if (window.webkitURL != undefined) { // webkit or chrome
        url = window.webkitURL.createObjectURL(file);
    }
    return url;
  }
  
}